package com.ruoyi.web.controller.gwc;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SnowFlakeIDGenerator;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.IDriverInfoService;
import com.ruoyi.system.service.IVehicleManagementService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gwc.domain.TVehicleApplication;
import com.ruoyi.gwc.service.ITVehicleApplicationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用车申请Controller
 * 
 * @author wp
 * @date 2024-11-18
 */
@RestController
@RequestMapping("/gwc/application")
public class TVehicleApplicationController extends BaseController{
    @Autowired
    private ITVehicleApplicationService tVehicleApplicationService;

    @Autowired
    private IDriverInfoService driverInfoService;
    @Autowired
    private IVehicleManagementService vehicleManagementService;

    /**
     * 查询用车申请列表
     */
    @ApiOperation("查询用车申请列表")
    @GetMapping("/list")
    public TableDataInfo list(TVehicleApplication tVehicleApplication)
    {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (!loginUser.getUser().isAdmin() && !StringUtils.isNotNull((tVehicleApplication.getCompanyId()))) {
            tVehicleApplication.setCompanyId(loginUser.getUser().getDeptId());
        }
        List<TVehicleApplication> list = tVehicleApplicationService.selectTVehicleApplicationList(tVehicleApplication);
        //0：已申请，1：已审批，2已调度，3已接单，4已出车，5已还车，6撤销订单 7已驳回

        return getDataTable(list);
    }

    /**
     * 导出用车申请列表
     */
    @ApiOperation("导出用车申请列表")
    @Log(title = "导出用车申请列表", businessType = BusinessType.EXPORT)
    @ApiOperationSupport(includeParameters = {"companyName", "applyName", "applyTel", "useName", "useTel", "purpose", "remark", "boardingLocation","destination",
            "vehicleNum","userNum","boardingTime","returnTime","numberPlate"})
    @PostMapping("/export")
    public void export(HttpServletResponse response, TVehicleApplication tVehicleApplication)
    {
        List<TVehicleApplication> list = tVehicleApplicationService.selectTVehicleApplicationList(tVehicleApplication);
        ExcelUtil<TVehicleApplication> util = new ExcelUtil<TVehicleApplication>(TVehicleApplication.class);
        util.hideColumn("updateTime");
        util.hideColumn("updateBy");
        util.hideColumn("remark");
        util.exportExcel(response, list, "用车申请数据");
    }

    /**
     * 获取用车申请详细信息
     */
    @ApiOperation("获取用车申请详细信息")
    @GetMapping(value = "/{applyId}")
    public AjaxResult getInfo(@PathVariable("applyId") Long applyId)
    {
        return success(tVehicleApplicationService.selectTVehicleApplicationByApplyId(applyId));
    }

    /**
     * 新增用车申请
     */
    @Log(title = "用车申请", businessType = BusinessType.INSERT)
    @ApiOperation("新增用车申请")
    @PostMapping
    public AjaxResult add(@RequestBody TVehicleApplication tVehicleApplication)
    {
        tVehicleApplication.setApplyId(SnowFlakeIDGenerator.getInstance().nextId());
        tVehicleApplicationService.save(tVehicleApplication);
        return success();
    }

    /**
     * 修改用车申请
     */
    @Log(title = "用车申请", businessType = BusinessType.UPDATE)
    @ApiOperation("修改用车申请")
    @PutMapping
    public AjaxResult edit(@RequestBody TVehicleApplication tVehicleApplication)
    {
        //0：已申请，1：已审批，2已调度，3已接单，4已出车，5已还车，6撤销订单，7已驳回
        if (tVehicleApplication.getOrderStatus() == 5 || tVehicleApplication.getOrderStatus() == 6){
            //更新司机为空闲
            driverInfoService.updateDriverInfoByApplyId(tVehicleApplication.getApplyId());
            //更新车辆状态为空闲
            vehicleManagementService.updateVehicleByApplyId(tVehicleApplication.getApplyId());
        }
        return toAjax(tVehicleApplicationService.saveOrUpdate(tVehicleApplication));
    }

    /**
     * 删除用车申请
     */
    @Log(title = "用车申请", businessType = BusinessType.DELETE)
    @ApiOperation("删除用车申请")
	@DeleteMapping("/{applyIds}")
    public AjaxResult remove(@PathVariable Long[] applyIds)
    {
        return toAjax(tVehicleApplicationService.deleteTVehicleApplicationByApplyIds(applyIds));
    }

    @Log(title ="用车申请凭证")
    @ApiOperation("用车申请凭证")
    @PostMapping("/uploadVoucher")
    public AjaxResult uploadVoucher(@RequestBody MultipartFile[] file) {
        return success(uploadFile(file, "voucher"));
    }

    @ApiOperation("通过车辆id获取用车申请单列表")
    @GetMapping("/getVehicleApplicationList")
    public AjaxResult getVehicleApplicationList(@RequestParam Long vehicleId) {
        List<TVehicleApplication> list = tVehicleApplicationService.getVehicleApplicationList(vehicleId);
        return success(list);
    }

}
